<?php
if (!isset($_SESSION)) {
    session_start();
}
$cart = array();
if (isset($_SESSION["gh"])) {
    $cart = $_SESSION["gh"];
}
$id = "";
$sl = 1;

if (isset($_GET["sl"])) {
    $sl = floor($_GET["sl"] * 1);
}

if (isset($_GET["id"])) {
    $id = $_GET["id"];
    $act = "a";
}
if (isset($_GET["act"])) {
    $act = $_GET["act"];
}

switch ($act) {
    case "a":
        addCart($id, $sl);
        break;
    case "u":
        updateCart($id, $sl);
        break;
    case "d":
        deleteCart($id);
        break;
    case "tt":
        checkOut($conn, $cart, $_GET["hoten"], $_GET["diachi"], $_GET["email"], $_GET["dt"], $_GET["fax"], $_GET["cty"], date("Y-m-d H:i:s"));
        break;
}

$tongtien = 0;
var_dump ($cart);
echo count($cart);
die();
foreach ($cart as $key => $value) {
    $count = count($cart);
    if ($key != "") {
        $strSQL = "SELECT * FROM sanpham WHERE id='$key' ";
        $mysqli_result = mysqli_query($conn, $strSQL);
        $result = mysqli_fetch_array($mysqli_result);

    //    $id = $r["id"];
    //    $tensp = $r["tensp"];
    //    $gia = $r["gia"];
    //    $gia2 = number_format($gia, 0, '', '.');
        $tong = $result["gia"] * $sl;
        $tong2 = number_format($result["gia"], 0, '', '.');
        $tongtien+=$tong;
        $tongtien2 = number_format($tongtien, 0, '', '.');
    }
}

function checkOut($conn, $cart, $name, $address, $email, $phoneNumber, $fax, $company, $now) {
    if ($name == "" || $address == "" || $email == "" || $phoneNumber == "")
        echo "Quý khách phải nhập đầy đủ thông tin vào những nơi có dấu *";
    else {
        foreach ($cart as $id => $sl) {
            $q = mysqli_query($conn, "SELECT gia FROM sanpham WHERE id='$id'");
            while ($rq = mysqli_fetch_array($q)) {
                $gia = $rq["gia"];
                $tien = $gia * $sl;
                $sql2 = "INSERT INTO hoadon(hoten,diachi,email,dienthoai,fax,cty,id,soluong,tongtien,ngaydat,tinhtrang) VALUES ('$name','$address','$email','$phoneNumber','$fax','$company','$id',$sl,'$tien','$now','checkOut')";
                $sql2.=';';
                $ketqua2 = mysqli_query($conn, $sql2);

                echo "Quý khách đã gửi đơn hàng thành công!";
            }
            session_destroy();
        }
    }
}
?>


<table width=560 border=0 cellspacing=0 cellpadding=0 style="border:1px solid #333">
    <tr>
        <td colspan=6 class=tieude align=center>GIỎ HÀNG CỦA QUÝ KHÁCH</td>
    </tr>
    <tr bgcolor="#ad2200" align=center height=30 style="color:white">
        <td width="80" style="border-right:1px solid #666"><b>Sản phẩm</b></td>
        <td width="60" style="border-right:1px solid #666"><b>Số lượng</td>
        <td width="85" style="border-right:1px solid #666"><b>Giá</td>
        <td width="85" style="border-right:1px solid #666"><b>Tổng</td>
        <td width="50" style="border-right:1px solid #666"><b>Cập Nhật</td>	
        <td width="50" ><b>Xóa</td>                
    </tr>
    <?php foreach ($result as $key => $value ) { ?> 
    <tr align="center" height="30" >

        <td style="border-right:1px solid #666; border-bottom:1px solid #666"><?php echo $value['tensp']; ?></td>
        <td style="border-right:1px solid #666; border-bottom:1px solid #666">
            <form id='f<?php echo $value['id'] ?>'>
                <input type=hidden name='act' /><input type=hidden name=id value='$id' />
                <input type=hidden name=b value='giohang'>
                <input type="text" name="sl" value='$sl' style="width:30px" />  
        </td></form>
        <td align="right" style="border-right:1px solid #666; border-bottom:1px solid #666; padding-right:3px"><?php echo number_format($value['gia'], 0, '', '.') . " VND" ?></td>
        <td align="right" style="border-right:1px solid #666; border-bottom:1px solid #666; padding-right:3px"><?php echo number_format($value["gia"] * $sl, 0, '', '.')." VND" ?></td>
        <td style=" border-bottom:1px solid #666; border-right:1px solid #666" >
            <a onClick="subMitF('f$id', 'u');">Cập Nhật</a>
        </td>               
        <td style=" border-bottom:1px solid #666" >
            <a onClick="subMitF('f$id', 'd');">Xóa</a>
        </td>               
    </tr>
    <?php }
    if ($count == "") { ?>
    <tr><td height=30 colspan=6 align=center style="padding-right:5px; padding-bottom:5px; color:#F00">Không có sản phẩm nào trong giỏ hàng của Quý khách!</td></tr>
    <?php } else { ?>
    <tr>
        <td height=30 colspan=6 align=right style="padding-right:5px; padding-bottom:5px; color:#F00">Tổng số tiền phải thanh toán: $tongtien2 VND</td></tr>
    <tr>
        <td colspan="6" bgcolor="#fff" align="center" height="35">
            <form name=form>
                <input type="button" name="tieptucmuahang" value="Tiếp Tục Mua Hàng" class="button3" onmouseover="style.background = 'url(images/button-150-2-o.png)'" onmouseout="style.background = 'url(images/button-150-o.png)'" onclick="document.form.action = 'index.php';
                        document.form.submit();" />
                <input type="button" name="checkOut" value="Đặt Hàng" class="button2" onmouseover="style.background = 'url(images/button-110-2-o.png)'" onmouseout="style.background = 'url(images/button-110-o.png)'" onclick="document.getElementById('thanhtoan').style.display = 'block'"/>
            </form>
        </td>
    </tr>
    <?php } ?>
</table>
<div id='thanhtoan' style='display:none'>
    <form id='k$id'>
        <table width="560" border="0" cellspacing="0" cellpadding="0" style="padding-top:10px; border:1px solid #333">		
            <td colspan="6" class="tieude" align="center">THÔNG TIN LIÊN HỆ</td>
            </tr>
            <tr>
                <td height="30"><div style="padding-left:70px">Họ và tên: </div></td>
                <td width="350">		
                    <input type=hidden name='act' /><input type=hidden name=id value='$id' />
                    <input type=hidden name=b value='giohang'>
                    <input name="hoten" type="text" size="35" maxlength="50" > <font color="#FF0000">*</font></td>
            </tr>
            <tr>
                <td height="30"><div style="padding-left:70px">Địa chỉ:</div></td>
                <td><input name="diachi" type="text" size="35" maxlength="50"> <font color="#FF0000"> *</font> </td>
            </tr>
            <tr>
                <td height="30"><div style="padding-left:70px">Email:</div> </td>
                <td><input name="email" type="text" size="35" maxlength="50"> <font color="#FF0000"> *</font></td>
            </tr>
            <tr>
                <td height="30"><div style="padding-left:70px">Số điện thoại:</div></td>
                <td><input name="dt" type="text" size="35" maxlength="50" onkeyup="valid(this, 'numbers')" onblur="valid(this, 'numbers')" ><font color="#FF0000"> *</font></td>
            </tr>
            <tr>
                <td height="30"><div style="padding-left:70px">Fax:</div></td>
                <td><input name="fax" type="text" size="35" maxlength="50" onkeyup="valid(this, 'numbers')" onblur="valid(this, 'numbers')"></td>
            </tr>
            <tr>
                <td height="30"><div style="padding-left:70px">Công ty:</div></td>
                <td><input name="cty" type="text" size="35" maxlength="50"></td>
            </tr>
            <tr>
                <td colspan="2" bgcolor="#fff" align="center" height="35">
                    <input type="button" value="Gửi" class="button" onmouseover="style.background = 'url(images/button-2-o.gif)'" onmouseout="style.background = 'url(images/button-o.gif)'" onclick="subMitF('k$id', 'tt');">
                    <input type="reset" value="Nhập lại" class="button" onmouseover="style.background = 'url(images/button-2-o.gif)'" onmouseout="style.background = 'url(images/button-o.gif)'"  >
                </td>
            </tr>	  
        </table>
    </form>
</div>

<script type="text/javascript">
    function subMitF(fn, type) {
        var a = document.getElementById(fn);
        b = a.getElementsByTagName('input')[0];
        b.value = type;
        a.submit();

    }
</script>
